﻿@Cofe ?= {}

$ = jQuery
Entry = Cofe.Entry
EntryItemList = Cofe.EntryItemList
EntryItem = Cofe.EntryItem
EntryList = Cofe.EntryList
Link = Cofe.Link
utils = Cofe.utils

# CONTROLLER - Base class for represent an Entry (controller) as Tree item.
#     params - @el, @url, @entryItem, @treeView
#     events - expaned, collapsed
#
class EntryTreeItem extends Spine.Controller  
  tag : 'li'

  elements : 
    #".treeItemRoot:first" : "uiRoot"
    ".header:first" : "uiHeader"
    ".header:first .label" : "uiLabel"
    ".subEntries:first" : "uiSubEntries"    
    ".toggleExpand:first" : "uiToggleExpand"
    
   

  #events :
    #"click .label:first" : "selectCurrent"
    #"click .toggleExpand:first" : "toggleExpand"

  
  loaded : false
  selected : false
  state : "collapsed"

  init : ->
    if @entry is undefined then throw "entry is not defined"
    if @treeView is undefined then throw "treeView is not defined"
    
    @entryItemList = @treeView.constructEntryItemList(undefined)  
    @entryItemList.bind('error', @treeView.error)
    @checkBounty()

  expand : ->
    if @state is "collapsed"
      if !@loaded
        @state = "expanding"
        @entryItemList.changeUrlPath(utils.formatPath(@entry.path, { rel : "list", filetype : "folder" }))
        @entryItemList.bind("refreshed", @refreshed)
        @loaded = true
      else 
        @state = "expanded"

      @refresh(false)
      @trigger("expanded", @)

  collapse : ->
    if @state isnt "collapsed"
      @state = "collapsed"
      @refresh(false)
      @trigger("collapsed", @)

  refreshed : () =>
    #console.log(@)
    @state = "expanded"
    @entryItemList.unbind("refreshed")
    #console.log @state + @entry.path
    @refresh(true)
  
  refresh : (refreshItemList) =>
    utils.changeState(@state, @treeView.treeItemStates, @uiToggleExpand)

    if @selected
      @uiLabel.addClass("ui-selected")
    else @uiLabel.removeClass("ui-selected")

    switch @state
      when "expanded"
        @uiSubEntries.show()
        if refreshItemList
           @entryItemList.el = @uiSubEntries
           @entryItemList.render()
      when "collapsed"
        @uiSubEntries.hide()     

    @el
  
  render : =>
    @el.html(@template())
    @uiHeader = $(@uiHeader.selector, @el)
    @uiHeader.html(@templateHeader())

    @uiLabel = $(@uiLabel.selector, @el)
    @uiSubEntries = $(@uiSubEntries.selector, @el)
    @uiToggleExpand = $(@uiToggleExpand.selector, @el)

    @uiToggleExpand.on("click", null, null, @toggleExpand)    
    @uiHeader.on("click", null, null, @selectCurrent)    
      
    @refresh(false)
    @el

  

  remove: () =>
    @el.remove()

  template: () ->    
    @treeView.childItemTemplate.render(@entry)

  templateHeader : () ->    
    @treeView.childHeaderTemplate.render(@entry)
  
  getBountyType : () ->
    if @treeView.bountyPath is undefined then return "n/a"
    if @treeView.bountyPath is @entry.path then return "bounty"
    if @treeView.bountyPath.indexOf(@entry.path) is 0 then return "bountyParent"
    if @entry.path.indexOf(@treeView.bountyPath) is 0 then return "bountyChild"    
    return "other"

  checkBounty : () ->
    #console.log(@entry.path + "-" + @getBountyType())
    bountyType = @getBountyType()
    switch bountyType
      when "other" 
        @collapse()
      when "bounty"
        @expand()
        @treeView.collectBounty(@)
      when "bountyParent"
        @expand()
        if @entryItemList.folderItems isnt undefined
          matched = f for f in @entryItemList.folderItems when f.getBountyType() isnt "other"
          if not matched
            @treeView.collectBounty(undefined)
          else 
            matched.checkBounty() 
      when "bountyChild"
        @collapse()
     
    
    if @folderItems isnt undefined
      for subFolder in @folderItems
        subFolder.checkBounty()
    @

  selectCurrent : =>
    #console.log("selectCurrent")
    @treeView.selected(@)

  toggleExpand : =>
    log.debug("EntryTreeItem ToggleExpand - " + @entry.path)
    if @state is "expanding" or @state is "expanded"
      @collapse()       
    else
      @expand()

  Cofe.EntryTreeItem = EntryTreeItem